$(function() {

//    if ($("#dest_hot").length != 0) {

    /***********************************************************
     * JQUERY UI Autocomplete Combobox
     ***********************************************************/
    $('#dest_hot').on('focus', function() {
        if ($(this).val() === lang['search.search_placeholder']) {
            $(this).val('');
            $(this).removeClass('idle');
        }
    });
    $('#dest_hot').on('blur', function() {

        if ($(this).val() === '') {
            $(this).addClass('idle');
            $(this).val(lang['search.search_placeholder']);
        }

    });


    /***********************************************************
     * JQUERY UI Autocomplete
     ***********************************************************/
    API.Form.AutoComplete.Init("#dest_hot", false, true);


    /***********************************************************
     * JQUERY UI Calendar
     ***********************************************************/
    API.Form.DatePicker.Init("check_in", "check_out", 1);

    if (typeof do_form2 !== 'undefined') {
        API.Form.DatePicker.Init("ckeck_in2", "check_out2", 1);
    }


    /***********************************************************
     * Childs Age handlers
     ***********************************************************/
    do_childs = function(nr, id) {

        nr = parseInt(nr);

        if (nr === 0) {
            $(".ages" + id).hide();
            reset_ch_inputs(id);

        } else {
            $(".ages" + id).show();

            for (i = 1; i <= nr; i++) {
                $(".ages" + id + " .age-" + i).show();
            }
            for (i = nr + 1; i <= 5; i++) {
                $(".ages" + id + " .age-" + i + " input").val("");
                $(".ages" + id + " .age-" + i).hide();
            }
        }
    };

    $(".positive-integer").numeric({
        decimal: false,
        negative: false
    });

    $("#nr_childrens").live("change", function() {
        do_childs($(this).val(), "");
    });
    $("#nr_childrens2").live("change", function() {
        do_childs($(this).val(), "2");
    });

    reset_ch_inputs = function(id) {
        for (i = 1; i <= 5; i++) {
            $(".ages" + id + " .age-" + i + " input").val("");
            $(".ages" + id + " .age-" + i).hide();
        }
    };

    // do resete childs and ages
    if (typeof doResetAges !== 'undefined') {

        $("#nr_childrens").val(0);
        reset_ch_inputs("");

        // set visible childs ages
        do_childs(nr_children, "");

        if (typeof do_form2 !== 'undefined') {

            $("#nr_childrens2").val(0);

            reset_ch_inputs("2");
            // set visible childs ages
            do_childs(nr_children2, "2");
        }

    }

    // do resete childs and ages
    if (typeof nr_children !== 'undefined') {
        do_childs(nr_children, "");
    }

    // do resete childs and ages
    if (typeof nr_children2 !== 'undefined') {
        do_childs(nr_children2, "2");
    }

    check_ages_form_submit = function(nr, id) {


        // do validation for childs age
        for (i = 1; i <= nr; i++) {

            if ($(".ages" + id + " .age-" + i + " input").val() == "") {
                return false;
            }
        }

        return true;

    }

    $('form.show_search_form').submit(function(eventData) {

        if (!$('#non_specific_dates').is(':checked')) {

            // do validation for childs age
            nr = $("#nr_childrens").val();

            if (!check_ages_form_submit(nr, "")) {
                $("#freeow").freeow("", "Please fill age(s) of children.", {
                    classes: ["error"],
                    autoHide: true,
                    autoHideDelay: 5000,
                    hideDuration: 1500
                });

                return false;
            }

        }

        eventData.preventDefault();

        var form = $('form.show_search_form');

        var lang_encoded = encodeURIComponent(lang['search.search_placeholder']);

        lang_encoded = lang_encoded.replace(/\%20/g, "+");// Uklanjam razmak iz lang stringa da bi pronasao vreijednost u serializovanom i zamjenio sa -1

        var data = form.serialize();
        data = data.replace(lang_encoded, '-1');

        var url = form.attr('action') + "?" + data;

        window.location = url;

    });

// Do form validation
    $('.available_form .btn').live("click", function() {

        // do validation for childs age
        nr = $("#nr_childrens2").val();

        if (!check_ages_form_submit(nr, "2")) {
            $("#freeow").freeow("", "Please fill age(s) of children.", {
                classes: ["error"],
                autoHide: true,
                autoHideDelay: 5000,
                hideDuration: 1500
            });

            return false;
        }
    });

});